﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WindowsTheme.Helper
{
    class FileHelper
    {
        /// <summary>
        /// 判断文件是否存在，存在返回true 如果不存在将会根据flag进行创建，默认不创建
        /// </summary>
        /// <param name="file_path">文件路径，不可为文件夹</param>
        /// <param name="Creat_flag">为true时自动创建文件</param>
        /// <returns></returns>
        public static bool File_existence(String file_path, bool Creat_flag = false)
        {
            if (File.Exists(file_path))
                return true;
            else
            {
                if (Creat_flag)
                {
                    try
                    {
                        File.Create(file_path).Dispose();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                    }

                }
                return false;

            }

        }
        /// <summary>
        /// 删除文件的辅助，需要文件路径
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static bool FileDelete(String FilePath)
        {
            if (File.Exists(FilePath))
            {
                try
                {
                    File.Delete(FilePath);
                }
                catch (Exception ex)
                {
                    PrintLog.Log(ex);
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 判断文件夹是否存在，以及不存在是否创建
        /// </summary>
        /// <param name="folder_path"></param>
        /// <param name="Creat_flag">为true时创建</param>
        /// <returns></returns>
        public static bool Folder_existence(String folder_path, bool Creat_flag = false)
        {

            if (Directory.Exists(folder_path))
            {
                return true;
            }
            else
            {
                if (Creat_flag)
                {
                    try
                    {
                        Directory.CreateDirectory(folder_path);
                        return true;
                    }
                    catch (Exception ex)
                    {
                        PrintLog.Log(ex);
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// 打开文件夹
        /// </summary>
        /// <param name="FileName"></param>
        public static void OpenFile(String FileName) {
            Folder_existence(FileName, true);
            System.Diagnostics.Process.Start("explorer.exe", FileName);

        }

        /// <summary>
        /// 检测图片是否符合要求，返回状态码：0（文件读取失败）  1 （图片比例不符合要求） 2 （图片宽高不符合要求）
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static String GetImgInfo(String FilePath) {
            String Tip = "";
            try
            {
                Image pic = Image.FromFile(FilePath);//strFilePath是该图片的绝对路径
                int intWidth = pic.Width;//长度像素值
                int intHeight = pic.Height;//高度像素值 
                
                float Proportion = (float)intWidth / intHeight;
               
                Console.WriteLine("图片宽{0}高{1}比例{2}文件名{3}", intWidth, intHeight, Proportion, FilePath);
                pic.Dispose();

                if (Proportion < 1.2)
                    Tip += string.Format("图片宽高比[{0}]不在正常文件区间。",Proportion);
                if (intWidth < 800 || intHeight < 600)
                    Tip += string.Format("图片宽高低于正常值，宽{0}高{1}", intWidth, intHeight);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return "文件打开失败！";
            }
            return Tip;
        }
        /// <summary>
        ///进行文本替换的类，读取并覆盖回源目录。需要文件路径，旧文本，新文本 其中旧文本默认支持正则
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="OldText">需要替换的旧文本，支持正则表达式</param>
        /// <param name="NewText"></param>
        /// <param name="RegexFalg">默认使用正则替换模式，如果不想启用请用false</param>
        /// <param name="DefaultEncoding"></param>
        /// <returns></returns>
        public static bool TextFileReplace(String FilePath,string OldText,string NewText,bool RegexFalg=true,Encoding DefaultEncoding=null) {
           
            try
            {
                String ReadText = "";
                if (DefaultEncoding != null)
                    ReadText = File.ReadAllText(FilePath, DefaultEncoding);
                else
                    ReadText = File.ReadAllText(FilePath, Encoding.ASCII);
                if(RegexFalg)
                ReadText = Regex.Replace(ReadText, OldText, NewText);
                else
                    ReadText = ReadText.Replace( OldText, NewText);
                File.WriteAllText(FilePath, ReadText);
                Console.WriteLine("已替换");
                return true;
            }
            catch (Exception e) {
                PrintLog.Log(e);
                return false;
            }

        }
        /// <summary>
        /// 复制文件的辅助类
        /// </summary>
        /// <param name="Filepath"></param>
        /// <param name="NewPath"></param>
        /// <returns></returns>
        public static bool FileCopy(String Filepath,String NewPath) {
            try
            {

                File.Copy(Filepath, NewPath,true);
                return true;
            }
            catch (Exception ex){
                PrintLog.Log(ex);
                PrintLog.Log("复制的路径{0}，新路径{1}",Filepath,NewPath);
            }
            return false;
        }
   
        public static bool FileWrite(String FileName,byte[] Filebyte) {


            try
            {
                FileStream fs = new FileStream(FileName, FileMode.Create);

                //将byte数组写入文件中
                fs.Write(Filebyte, 0, Filebyte.Length);
                //所有流类型都要关闭流，否则会出现内存泄露问题
                fs.Close();
                return true;
            }
            catch (Exception ex) {
                PrintLog.Log(ex);
                PrintLog.Log("写入的路径{0}", FileName);
                return false;
            }

        }
    }
}
